/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/1
 * Description:版权所有 QQ:376231445
 */
package com.it1997.it1997admin.service.impl;
import com.it1997.it1997admin.mapper.PermissionMapper;
import com.it1997.it1997admin.mapper.RoleMapper;
import com.it1997.it1997admin.pojo.Role;
import com.it1997.it1997admin.service.PermissionService;
import com.it1997.it1997admin.service.RoleService;
import com.it1997.it1997admin.vo.RoleVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Log4j2
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Role> getRoles(Integer userId) {
        return roleMapper.getRoles(userId);
    }

    @Override
    public List<Role> getAllRoles(Integer page,Integer limit) {
        Integer index=(page-1)*limit;
        Integer last=page*limit;
        return roleMapper.getAllRoles(index,last);
    }

    @Override
    public List<Role> getAllRolesList() {
        return roleMapper.getAllRolesList();
    }

    @Override
    public int addRole(RoleVo roleVo) {
        try {
            //根据角色名查询角色信息
            Role role = roleMapper.findRole(roleVo.getRoleName());
                log.info("根据角色名查询角色："+role);
                if (role == null) {
                    log.info("角色不存在就添加角色信息");
                //若角色不存在就添加角色信息
                int i = roleMapper.addRole(roleVo.getRoleName(), roleVo.getDescription());
                //查询出刚添加的角色信息
               Role role2 = roleMapper.findRole(roleVo.getRoleName());
                log.info("新增的角色信息："+role2);
                //为刚添加的角色赋予权限
                int j = permissionService.addPermission(roleVo.getPermissionList(), role2.getId());
                return 1;//角色不存在 插入成功
            }else if(role.getStatus()==0){
                log.info("用户存在被删除过   更新角色状态");
                //用户存在被删除过   更新角色状态
                role.setStatus(1);
                int h=roleMapper.updateRoleStatus(role);
                if(h>0){
                    return 1; //更新角色状态 更新成功
                }else {
                    return 2; //新增角色失败 未知异常
                }
            }
            return 0;
        } catch (Exception e) {
           log.error(e.getMessage());
            throw new RuntimeException();
        }
    }
    @Override
    public Role findRole(String roleName) {

        return roleMapper.findRole(roleName);
    }
    @Override
    public int updateRole(RoleVo roleVo) {
        try {
            log.info("角色id:"+roleVo.getId());
            //更新角色信息
            roleMapper.updateRole(roleVo);
            //清空该角色的权限信息
            roleMapper.deleteRoleAllPer(roleVo);
            //为用户设置新权限
            //查询角色是否拥有该权限
            String[] s = (roleVo.getPermissionList()).split(",");
            for (String s1 : s) {
                int i = roleMapper.findPerById(s1, roleVo.getId());
                log.info("权限是否存在：" + i);
                if (i == 1) {       //该角色拥有该权限 只需将status设为1即可
                    permissionMapper.updatePermissionStatus(s1, roleVo.getId());
                } else {    //该角色没有该权限 需要添加该权限
                    permissionMapper .addPermission(s1, roleVo.getId());
                }
            }
            return 1;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
    @Override
    public int deleteRole(List<RoleVo> roleVoList) {

        return roleMapper.deleteRole(roleVoList);
    }

    @Override
    public Integer getRoleById(Integer userId) {
        return roleMapper.getRoleById(userId);
    }
    @Override
    public Integer getRoleCount() {
        return roleMapper.getRoleCount();
    }

    @Override
    public RoleVo getRoleByRoleId(Integer roleId) {
      RoleVo roleVo=roleMapper.getRoleByRoleId(roleId);
        List<Integer> permissionId=permissionMapper.getPermissionId(roleId);
        String permissionList="";
        for (Integer integer : permissionId) {
            permissionList+=integer+",";
        }
        permissionList=permissionList.substring(0,permissionList.length()-1);
        roleVo.setPermissionList(permissionList);
        //log.info(roleVo);
        return roleVo;
    }

    @Override
    public List<RoleVo> searchRole(RoleVo roleVo) {
        Integer page=roleVo.getPage();
        Integer limit= roleVo.getLimit();
        Integer index=(page-1)*limit;
        Integer last=page*limit;
        return roleMapper.searchRole(roleVo,index,last);
    }

    @Override
    public Integer getSearchRoleCount(RoleVo roleVo) {
        return roleMapper.getSearchRoleCount(roleVo);
    }
}